package b1.scenes.fight
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.controls.tab.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.crime.*;
	import b1.scenes.home.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.Timer;
	import flash.media.Sound;

	import com.greensock.*;

	public class PackageNavigator extends TabController
	{
		//记录当前伸缩的状态
		public var extendFlag:Boolean;
		public var extendButton:BasicIcon; 

		private var downArrowButton:BasicIcon;
		private var upArrowButton:BasicIcon;
		private static var instance:PackageNavigator;
		public function PackageNavigator()
		{
			//主要图标
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("BottomNavigatorBgr");
			var BoneIcon:Class = ResourceHelper.getInstance().getClassByName("BoneIcon");
			var ArmIcon:Class = ResourceHelper.getInstance().getClassByName("ArmIcon");
			var Launcher:Class = ResourceHelper.getInstance().getClassByName("Launcher0");
			super(TabControllerBackground, null, 2);
			var UpArrow:Class = ResourceHelper.getInstance().getClassByName("BottomNavigatorUpArrow");
			upArrowButton = new BasicIcon(UpArrow);
			upArrowButton.x = 700;
			upArrowButton.y =  22;
			upArrowButton.scaleY = -1;
			addChild(upArrowButton);
			upArrowButton.addEventListener(MouseEvent.CLICK, onUpDownArrowButtonClick);
			extendButton = upArrowButton;
			extendFlag = true;
			//标签页
			var tab:Tab = new Tab("骨头", 10, 1, 5, 2);
			this.addTab(BoneIcon, tab);
			tab = new Tab("道具", 10, 1, 5, 2);
			this.addTab(ArmIcon, tab);
			tab = new Tab("装备", 10, 1, 5, 2);
			this.addTab(Launcher, tab);
			//标签图标的事件（扩展自己）
			for (var i:int = 0; i < tabTitles.length; i++)
			{
				this.tabTitles[i].addEventListener(MouseEvent.CLICK, onTabIconClick);
			}
			
		}
		
		private function onUpDownArrowButtonClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			refresh();
			switchExtendStatus();
		}
		
		public function onTabIconClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			refresh();
			extend();
		}
		
		public function extend():void
		{
			if (!extendFlag)
			{
				switchExtendStatus();
			}
		}
		
		public function hide():void
		{
			if (extendFlag)
			{
				switchExtendStatus();
			}
		}
		
		//转换当前的伸缩状态
		public function switchExtendStatus():void
		{
			//添加新手导引
			if (SceneContainer.getInstance().step == 23)
			{
				SceneContainer.getInstance().addStep();
			}
			
			var extendOffset:int = 80;
			if (extendFlag)
			{
				this.y += extendOffset;
				upArrowButton.scaleY = 1;
				upArrowButton.y =  22;
				extendFlag = false;
			}
			else
			{
				this.y -= extendOffset;
				upArrowButton.scaleY = -1;
				upArrowButton.y =  22 + upArrowButton.height - 15;
				extendFlag = true;

				//播放音效
				var SoundButton1:Class = ResourceHelper.getInstance().getClassByName("SoundButton1");
				var sound:Sound = new SoundButton1();
				sound.play();
			}
		}
		
		public function refresh():void
		{
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			for (var i:int; i < this.tabs.length; i++)
			{
				this.tabs[i].removeAll();
			}
			var objectItems:Array = new Array();
			for (var objectIndex:String in player.userPackage.objectList)
			{
				//战斗场景的包裹中不添加金骨头，index为1
				if (parseInt(objectIndex) != 1)
				{
					var objectNum:int = player.userPackage.objectList[objectIndex];
					if (objectNum > 0)
					{
						objectItems.push(objectIndex);
					}
				}
			}
			objectItems.sort(sortObjectsOnWeightAndPrice);
			for (var j:int = 0; j < objectItems.length; j++)
			{
				var index:int = objectItems[j];
				objectNum = player.userPackage.objectList[index];
				var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
				var objectItem:ObjectItem = new ObjectItem(TabItemBackground, index, objectNum, 1);
				var objectConfig:ObjectConfig = Config.objectConfigs[index];
				if (!objectConfig.noShowInFightFlag)
				{
					//骨头、道具
					if (objectConfig.type < 2) 
					{
						this.tabs[objectConfig.type].addItem(objectItem, objectItem.width, objectItem.height);
					}
					//弓
					else if (objectConfig.type == 3)
					{
						this.tabs[2].addItem(objectItem, objectItem.width, objectItem.height);
					}
				}
			}
 
		}
		
		public static function getInstance():PackageNavigator
		{
			if (!instance)
				instance = new PackageNavigator();
			return instance;
		}
		
		public function open(parentScene:Sprite):void
		{
			refresh();
			if (!parentScene.contains(this))
			{
				this.x = -25;
				this.y = 535;
				TweenLite.from(this, 1, {delay: 2, y:BFont.stageHeight, alpha:0});
				upArrowButton.scaleY = 1;
				upArrowButton.y =  22;
				extendFlag = false;
				parentScene.addChild(this);
			}
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
	}
}